package com.Utils.Tools;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.KeySpec;
import javax.xml.bind.DatatypeConverter;

public class SignEncrypt {

    // MD5 加密方法
    public static String getMD5utf8(String input) {
        try {
            // 获取 MD5 加密算法实例
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 将输入字符串转换为字节数组
            byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);


            // 计算 MD5 哈希值
            byte[] hashBytes = md.digest(inputBytes);

            // 将哈希值转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : hashBytes) {
                sb.append(String.format("%02x", b)); // 使用 %02x 将每个字节表示为两位十六进制数
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            // 当算法不可用时，处理异常
            System.out.println("MD5 算法不可用：" + e.getMessage());
            return null;
        }
    }

    // 3DES 加密方法
    public static String encrypt3DES(String originalString, String key) {
        try {

            // 将原始字符串转换为GBK编码的字节数组
            byte[] gbkBytes = originalString.getBytes(Charset.forName("GBK"));

            // 将密钥字符串转换为字节数组
            byte[] keyBytes = key.getBytes();

            // 创建 DESedeKeySpec 对象，并使用 DESede 算法生成 SecretKey
            KeySpec keySpec = new DESedeKeySpec(keyBytes);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);

            // 创建 Cipher 对象并指定运算模式和填充模式
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);

            // 加密字节数组
            byte[] encryptedBytes = cipher.doFinal(gbkBytes);

            // 将加密后的字节数组转换为十六进制字符串
            return DatatypeConverter.printHexBinary(encryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 3DES 解密方法
    public static String decrypt3DES(String encryptedString, String key) {
        if (encryptedString == null || key == null || key.length() != 24) {
            System.out.println("非法参数：密文或密钥无效");
            return null;
        }

        try {
            byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
            KeySpec keySpec = new DESedeKeySpec(keyBytes);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);

            // 指定解密算法、运算模式和填充模式
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");

            // 初始化Cipher对象为解密模式
            cipher.init(Cipher.DECRYPT_MODE, secretKey);

            // 将输入的16进制密文转换为字节数组
            byte[] encryptedBytes = hexStringToByteArray(encryptedString);

            // 执行解密
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

            // 将解密后的字节转换为GBK编码的字符串
            return new String(decryptedBytes, Charset.forName("GBK"));

        } catch (Exception e) {
            System.out.println("解密失败：" + e.getMessage());
            return null;
        }
    }




    public static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i + 1), 16));
        }
        return data;
    }



}
